package com.abberbex;

import org.newdawn.slick.AngelCodeFont;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.Sound;
import org.newdawn.slick.geom.Rectangle;
import org.newdawn.slick.state.BasicGameState;
import org.newdawn.slick.state.StateBasedGame;

/**
 * 
 * @author Brendan Dickerson
 *
 */
public class MenuState extends BasicGameState {

	private final int stateID;
	protected int windowHeight = 800;
	protected int windowWidth = 800;
	private Image menuBackground;
	private Image playButton;
	private float pbx = 300;
	private float pbw = 200;
	private float pbh = 100;
	private float pby = 375;
	private StateBasedGame game;
	private Rectangle pbp;
	private AngelCodeFont font;
	private Image logo;
	Rectangle r;
	private Image about;
	private Sound s;
	private Sound click;


	public MenuState(int ID)
	{
		this.stateID= ID;
	}

	@Override
	public void init(GameContainer cont, StateBasedGame game)
			throws SlickException {
		click=new Sound("audio/click.ogg");
		s=new Sound("audio/soundtrack.ogg");
		s.loop();
		menuBackground = new Image("gui/bkg.png");
		playButton = new Image("gui/playbutton.png");
		this.game=game;
		pbp = new Rectangle(pbx, pby, pbw, pbh);
		logo = new Image("gui/logo.png").getScaledCopy(2.5f);
		font = new AngelCodeFont("font/font.fnt", "font/font.png");
		about = new Image("gui/playbutton.png").getScaledCopy(.898989f);
	}

	@Override
	public void render(GameContainer cont, StateBasedGame game, Graphics g)
			throws SlickException {
		cont.setVSync(true);
		r = new Rectangle(pbx+about.getWidth()/4+4,pby+pbh,about.getWidth(),about.getHeight());

		menuBackground.draw(0, 0, windowHeight, windowWidth);

		if(cont.getInput().getMouseX()<pbx+pbw&&
				cont.getInput().getMouseX()>pbx&&
				cont.getInput().getMouseY()<pby+pbh&&
				cont.getInput().getMouseY()>pby)
		{
			playButton.draw(pbx, pby, pbw, pbh, Color.gray);
			font.drawString(pbx+64, pby+22, "Play", Color.white);
			about.draw(pbx+about.getWidth()/4+4,pby+pbh);
			font.drawString(pbx+56, pby+pbh+8, "About", Color.black);

		}
		else if(r.contains(cont.getInput().getMouseX(),cont.getInput().getMouseY()))
		{
			playButton.draw(pbx, pby, pbw, pbh);
			font.drawString(pbx+66, pby+22, "Play", Color.black);
			about.draw(pbx+about.getWidth()/4+4,pby+pbh,Color.gray);
			font.drawString(pbx+56, pby+pbh+8, "About", Color.white);

		}
		else
		{
			playButton.draw(pbx, pby, pbw, pbh);
			font.drawString(pbx+66, pby+22, "Play", Color.black);
			about.draw(pbx+about.getWidth()/4+4,pby+pbh);
			font.drawString(pbx+56, pby+pbh+8, "About", Color.black);


		}

		
		logo.draw(cont.getWidth()/4-128, cont.getHeight()/4+16);

	}

	@Override
	public void update(GameContainer cont, StateBasedGame game, int delta)
			throws SlickException {
		pbp.setBounds(pbx, pby, pbw, pbh);

	}



	public void mouseClicked(int button, int x, int y, int clickCount)
	{
		LevelOneState l = new LevelOneState(1);
		if(x<pbx+pbw&&x>pbx)
		{
			if(y<pby+pbh&&y>pby)
			{
				click.play(1,20);
				//EnterPlayState
				game.enterState(l.getID());
			}
		}
		if(r.contains(x,y))
		{
			click.play(1,20);
			String url = "http://code.google.com/p/movement/";
			String os = System.getProperty("os.name").toLowerCase();
		        Runtime rt = Runtime.getRuntime();
		 
			try{
		 
			    if (os.indexOf( "win" ) >= 0) {
		 
			        rt.exec( "rundll32 url.dll,FileProtocolHandler " + url);
		 
			    } else if (os.indexOf( "mac" ) >= 0) {
		 
			        rt.exec( "open " + url);
		 
		            } else if (os.indexOf( "nix") >=0 || os.indexOf( "nux") >=0) {
		 

			        String[] browsers = {"epiphany", "firefox", "mozilla", "konqueror",
			       			             "netscape","opera","links","lynx"};
		 
			        StringBuffer cmd = new StringBuffer();
			        for (int i=0; i<browsers.length; i++)
			            cmd.append( (i==0  ? "" : " || " ) + browsers[i] +" \"" + url + "\" ");
		 
			        rt.exec(new String[] { "sh", "-c", cmd.toString() });
		 
		           } else {
		                return;
		           }
		       }catch (Exception e){
			    return;
		       }
		      return;		
		}
	}

	@Override
	public int getID() {
		// TODO Auto-generated method stub
		return stateID;
	}



}
